// Author: combacsa@kaist.ac.kr
// http://www.jungol.co.kr/prog/Hanal/hanalView.php?qs_code=1031

//#define COMPILE
// 諛곗뿴 �섎굹濡� �닿구 �대뼸寃뚮뱺 �앸궡寃� �섎뒗 嫄� 援됱옣�� �щ��덈뒗 �쒕룄�� 寃껋씠��.
#ifdef COMPILE

#include <fstream>

using namespace std;

int data[5][5];
int bingo[25];
int answer;
bool used[5][5];

const int dir[12][4] = {{0, 0, 1, 0},
    {0, 1, 1, 0},
    {0, 2, 1, 0},
    {0, 3, 1, 0},
    {0, 4, 1, 0},
    {0, 0, 0, 1},
    {1, 0, 0, 1},
    {2, 0, 0, 1},
    {3, 0, 0, 1},
    {4, 0, 0, 1},
    {0, 0, 1, 1},
    {0, 4, 1, -1}};

void input() {
    ifstream in("input.txt");
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            in >> data[i][j];
        }
    }
    for (int k = 0; k < 25; k++) {
        in >> bingo[k];
    }
    in.close();
}

int solve() {
    for (int k = 0; k < 25; k++) {
        int lines = 0;
        for (int y = 0; y < 5; y++)
            for (int x = 0; x < 5; x++)
                if (data[y][x] == bingo[k])
                    used[y][x] = true;
        
        for (int l = 0; l < 12; l++) {
            int count = 0;
            int i = dir[l][0];
            int j = dir[l][1];
            for (int m = 0; m < 5; m++) {
                if (used[i][j])
                    count++;
                else
                    break;
                i += dir[l][2];
                j += dir[l][3];
            }
            if (count == 5)
                lines++;
        }
        if (lines >= 3)
            return k + 1;
        
    }
    // Must not reach here, logically ...
    return 25;
}

void output() {
    ofstream out("output.txt");
    out << solve();
    out.close();
}

int main() {
    input();
    
    
    output();
    return 0;
}


#endif
